package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.extra.im.ChatData;
import com.gouaixin.bee.beeframework2.tool.mLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

import cn.bmob.v3.BmobPushManager;
import cn.bmob.v3.BmobUser;


/**
 * Created by Administrator on 2015/3/16.
 */

//应用外，弹出的聊天对话框
public class JiqiMessadialogActivity extends Activity {
    Context context = JiqiMessadialogActivity.this;
    View view;

    String fid;//原来的发送方的id

    BmobPushManager bmobPushManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFinishOnTouchOutside(false);

                bmobPushManager = new BmobPushManager(this);

        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //初始化布局
        initLayout();

        //接受intent传过来的json
        initIntent();


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Intent it = intent;
        String id = it.getStringExtra("jid");
        String name = it.getStringExtra("jname");
        String icon = it.getStringExtra("icon");
        String qianming = it.getStringExtra("qianming");
        String text = it.getStringExtra("text");


        fName.setText(name);
        fText.setText(text);
    }

    /**
     * 接受intent传过来的json
     */
    String id;
    String name;
    String icon;
    String qianming;
    String text;


    private void initIntent() {
        Intent it = getIntent();

         id = it.getStringExtra("jid");
         name = it.getStringExtra("jname");
         icon = it.getStringExtra("icon");
         qianming = it.getStringExtra("qianming");
         text = it.getStringExtra("text");


        fName.setText(name);
        fText.setText(text);


    }

    /**
     * 初始化布局，按钮的监听
     */
    TextView fName;
    TextView fText;
    TextView send;
    EditText editText;

    private void initLayout() {
        view = LayoutInflater.from(context).inflate(R.layout.dialog_liaotiakuang, null);
        setContentView(view);


        fName = (TextView) view.findViewById(R.id.fName);//发送方的昵称
        fText = (TextView) view.findViewById(R.id.fText);//发送方的文本

        send = (TextView) view.findViewById(R.id.fText);//发送
        editText = (EditText) view.findViewById(R.id.input);//输入框

//        editText.setEnabled(false);

//        LinearLayout ss = (LinearLayout) findViewById(R.jid.root);

        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tiaozhuan();

            }
        });


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tiaozhuan();



            }
        });


    }

    public void tiaozhuan(){
        mLog.e("点击发送" + id + name + icon + qianming);
        Intent it = new Intent().setClass(context, C1_LiaotianJiemianTestActivity.class);
        it.putExtra("jid", id);
        it.putExtra("jname", name);
        it.putExtra("icon", icon);
        it.putExtra("qianming", qianming);
        it.putExtra("flag", "0");
        startActivity(it);
        finish();


    }


    /**
     * 发送消息的具体方法
     */
    private void sendMessage(Context context, String fText) {


        ChatData chatData = new ChatData();
        chatData.setContent(fText);
        chatData.setFname(BmobUser.getCurrentUser(context, USER.class).getName());
        chatData.setTime(new Date().getTime() + "");
        chatData.setFid(BmobUser.getCurrentUser(context).getUsername());
        chatData.setTid(fid);
        String gsonstr = new Gson().toJson(chatData);

        mLog.e("我用gosn生成的json:" + gsonstr);

        try {
            bmobPushManager.pushMessageAll(new JSONObject(gsonstr));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

//        long currentTime = System.currentTimeMillis();
//
//        if (keyCode == KeyEvent.KEYCODE_BACK && currentTime - oldTime > 3 * 1000) {
//            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
//            oldTime = System.currentTimeMillis();
//            return true;
//        }
        return true;
    }
}

